﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using GenericLibrariesToolKit;
using Model;
using BLL.Info.Impl;
using BLL.Info;
using BLL.Privilege;
using BLL.Privilege.Impl;
using Utils;

public partial class Page_Events_EventsList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
            showEventsList();
            //发表事件权限控制
            PostEventPrivilegeControl();
            

    }
    protected void btnEventsListPostSubmit_Click(object sender, ImageClickEventArgs e)
    {
        
    }
    /// <summary>
    /// 显示事件列表
    /// </summary>
    public void showEventsList()
    {
        EventBO eventsBO = new EventBOImpl();
        //设置orderby查询条件
        Dictionary<string, string> orderBy = new Dictionary<string, string>();
        orderBy.Add("EventDate", "desc");

        DataSet datasetNews = eventsBO.GetDataSet(-1,-1,null,orderBy);
        gvEventsList.DataSource = datasetNews;
        gvEventsList.DataBind();
    }
    protected void btnEventsListPostSubmit_Click1(object sender, ImageClickEventArgs e)
    {
        //获取数据
        string eventTitle = txtEventsPostTitle.Text.ToString();
        string eventkey = txtEventsPostKeywords.Text.ToString();
        string eventAddress = txtEventsPostLocation.Text.ToString();
        string eventDescription = txtEventsPostSummary.Text.ToString();
        string eventDate = txtEventsPostDate.Text.Trim();
        //封装数据
        Event events = new Event
        {
            EventTitle = eventTitle,
            EventKey = eventkey,
            EventAddress = eventAddress,
            EventDescription = eventDescription,
            EventPosterId = WebUtil.GetIdentifier(Session["userId"]),
            EventDate = eventDate,
        };
        //添加事件到数据库
        EventBO eventBO = new EventBOImpl();
        bool result = eventBO.Add(events);
        //清空文本框页面
        clearData();
        if (result)
        {
            //刷新
            showEventsList();
        }
    }
    /// <summary>
    ///对发表事件进行权限控制
    /// </summary>
    public void PostEventPrivilegeControl()
    {
        //获取用户ID
        string userID = WebUtil.GetIdentifier(Session["userId"]);
        /*--test--
        RoleModelRight roleModeRight = new RoleModelRightImpl();
        roleModeRight.GetRoleRight(userID);
        --test--*/
        bool result = isPostEventPrivilege(userID);
        //隐藏发表事件DIV,显示菜单栏DIV
        Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", GenerateScript(result));
    }

    /// <summary>
    /// 判断用户是否有发表事件的权限
    /// </summary>
    /// <returns></returns>
    public bool isPostEventPrivilege(string userID)
    {
        RoleRightBO roleRightBO = new RoleRightBOImpl();
        //如果用户为游客，返回false
        if (userID == null)
            return false;

        bool result = roleRightBO.IsPrivilege(userID, "eventsInfo", OperateType.add);
        return result;
    }
    /// <summary>
    /// 生成js脚本
    /// </summary>
    /// <param name="isShowLoginMaster"></param>
    /// <returns></returns>
    private string GenerateScript(bool isShowLoginMaster)
    {
        //存放script的容器
        StringBuilder script = new StringBuilder();
        script.Append("<script>");
        if (isShowLoginMaster)
        {
            script.Append("document.getElementById('PostEvent').style.display='block';");
        }
        else
        {
            script.Append("document.getElementById('PostEvent').style.display='none';");
        }
        script.Append("</script>");

        return script.ToString();
    }

    /// <summary>
    /// 分页处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvEventsList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.gvEventsList.PageIndex = e.NewPageIndex;
        this.gvEventsList.DataBind();
    }
    //返回主页面
    protected void btnEventsBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Write("<script language=javascript>history.go(-2);</script>");
    }
    //清空文本框页面
    private void clearData()
    {
        txtEventsPostTitle.Text="";
        txtEventsPostKeywords.Text="";
        txtEventsPostLocation.Text="";
        txtEventsPostSummary.Text="";
        txtEventsPostDate.Text = "";
    }
}